﻿package com.tvein.sanguo.swc.action.skill
{
	//flash
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextFormat;
	import fl.controls.Slider;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.OverLay;
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class Skill2 extends MovieClip
	{
		public static const NAME:String = "Skill-2";
		
		private var effectVol:int = 100;
		private var overLay:OverLay;

		private var roleEnergy:int;
		private var targetID:int;
		private var skillID:int;
		private var itemID:int;
		private var timeSlider:Slider;
		
		public function Skill2()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			overLay = new OverLay();
			this.addChild(overLay);
			this.setChildIndex(overLay, 0);
			
			txtSkillName.border = true;
			txtSkillName.borderColor = 0x333333;
			txtSkillRank.border = true;
			txtSkillRank.borderColor = 0x333333;
			txtSkillExp.border = true;
			txtSkillExp.borderColor = 0x333333;
			txtItemName.border = true;
			txtItemName.borderColor = 0x333333;
			
			timeSlider = new Slider();
			timeSlider.x = 250;
			timeSlider.y = 330;
			timeSlider.width = 240;
			timeSlider.height = 3;
			timeSlider.maximum = 100;
			timeSlider.minimum = 0;
			timeSlider.snapInterval = 10;
			timeSlider.tickInterval = 10;
			timeSlider.enabled = false;
			this.addChild(timeSlider);
			
			btnStart.visible = false;
			btnTimeMinus.visible = false;
			btnTimePlus.visible = false;
			
			btnClose.addEventListener(MouseEvent.CLICK, onClick);
			btnStart.addEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.addEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.addEventListener(MouseEvent.CLICK, onClick);
			
			mcFrame.reSize(600, 450);
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setTargetId":
					targetID = params.TargetId;
					break;
				case "setData":
					setData(params);
					break;
			}
		}
		
		private function setData(params:Object):void
		{
			for (var i:int = 0; i < params.length; i ++)
			{
				for (var k:String in params[i]) 
				{
					switch (k)
					{
						case "EnergyNum":
							roleEnergy = params[i][k];
							
							if (roleEnergy > 10)
							{
								timeSlider.value = 10;
								btnTimeMinus.visible = true;
								txtTime.text =  "（" + timeSlider.value + "分钟）";
					
								if (roleEnergy >= 2 * 10)
								{
									btnTimePlus.visible = true;
								}
								
								btnStart.visible = true;
							}
							else
							{
								txtTime.text =  "（精力值不足）";
							}
							break;
						case "SkillID":
							skillID = params[i][k];
							break;
						case "SkillName":
							txtSkillName.text = params[i][k];
							break;
						case "SkillType":
							break;
						case "SkillRank":
							txtSkillRank.text = params[i][k];
							break;
						case "SkillExp":
							txtSkillExp.text = params[i][k];
							break;
						case "ItemID":
							itemID = params[i][k];
							break;
						case "ItemName":
							txtItemName.text = params[i][k];
							break;
						case "ItemType":
							break;
						case "ItemRank":
							break;
					}
				}
			}
		}
		
		private function onClick(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			switch(evt.target.name)
			{
				case "btnClose":
					dispatchEvent(new SwcEvent(NAME, ["-close", "Skill-2"]));
					break;
				case "btnStart":
					dispatchEvent(new SwcEvent(NAME, ["-gameCommand", ["SkillCall(2,SkillUse," + targetID + ",skillId=" + skillID + "&itemId=" + itemID + "&actTime=" + timeSlider.value + ");"]]));
					break;
				case "btnTimeMinus":
					timeSlider.value -= 10;
					btnTimePlus.visible = true;
					
					if (timeSlider.value == 0)
					{
						btnTimeMinus.visible = false;
						btnStart.visible = false;
					}
					txtTime.text =  "（" + timeSlider.value + "分钟）";
					break;
				case "btnTimePlus":
					timeSlider.value += 10;
					btnTimeMinus.visible = true;
					btnStart.visible = true;
					
					if (timeSlider.value + 10 > roleEnergy)
					{
						btnTimePlus.visible = false;
					}
					txtTime.text =  "（" + timeSlider.value + "分钟）";
					break;
			}
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
			
			btnClose.removeEventListener(MouseEvent.CLICK, onClick);
			btnStart.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimeMinus.removeEventListener(MouseEvent.CLICK, onClick);
			btnTimePlus.removeEventListener(MouseEvent.CLICK, onClick);
		}

		private function _resizeUI(evt:Event = null):void
		{
			overLay.x = overLay.y = overLay.width = overLay.height = 0;
			
			this.x = (stage.stageWidth - this.width) * .5
			
			if ((stage.stageHeight - this.height) > 150)
			{
				this.y = (stage.stageHeight - this.height) * .3;
			}
			else
			{
				this.y = 45;
			}
			
			overLay.x = 0 - this.x;
			overLay.y = 0 - this.y;
			overLay.width = stage.stageWidth;
			overLay.height = stage.stageHeight;
		}
	}
}